package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
import models.User;
import tools.Encryptor;

public class LoginController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
        dispatcher.forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        String wachtwoord;
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();

        String loginnaam = new String();
        wachtwoord = new String();


        boolean correctlogin = false;
        String address = null;
        boolean erroratlogin = false;

        if (request.getParameter("loginnaam") != null) {
            loginnaam = request.getParameter("loginnaam");

            if (request.getParameter("wachtwoord") != null) {
                wachtwoord = request.getParameter("wachtwoord");
                wachtwoord = Encryptor.encrypt(wachtwoord);
                correctlogin = queryManager.dologincheck(loginnaam, wachtwoord);
            }else{correctlogin = false;}
        }else{correctlogin = false;}



        if (correctlogin == true) {
            User user;
            user = queryManager.getuser(loginnaam);

            if (user.getLevel() == 1) {
                address = "student_home.jsp";
            }
            if (user.getLevel() == 2) {
                address = "leraar_home.jsp";
            }
            if (user.getLevel() == 3) {
                address = "admin_home.jsp";
            }

            request.setAttribute("gebruikerinfo", user);
            sessie.setAttribute("gebruikerinfo", user);
            sessie.setAttribute("userId", user.getID());
            sessie.setAttribute("userLevel", user.getLevel());
            sessie.setAttribute("userName", user.getGebruikersnaam());
            sessie.setAttribute("tryOut", null);
        } else {
            erroratlogin = true;
            address = "login";
        }

        request.setAttribute("erroratlogin", erroratlogin);
        wachtwoord = null;
        loginnaam = null;

        dbManager.closeConnection();
        response.setContentType("text/plain");
        PrintWriter out= response.getWriter();
        response.setStatus(response.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", address);        
        //RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        //dispatcher.forward(request, response); //Stuurt door naar de Gebruikers index



    }
}
